package com.xiaoxiao.spring.web;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Slf4j
public class DispatcherServlet implements Servlet {
	@Override
	public void init(ServletConfig servletConfig) throws ServletException {

	}

	@Override
	public ServletConfig getServletConfig() {
		return null;
	}

	@Override
	public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
		log.info("my dispathcer servlet");
		HttpServletRequest request =  ((HttpServletRequest)servletRequest);
		String handlerUrl =request.getContextPath()+request.getRequestURI();
		log.info("requestUrl:{}",handlerUrl);
		Handler handler = HandlerMapping.getHandler(handlerUrl);
		if(handler==null){
			servletResponse.getWriter().print("404");
			return;
		}
		Object val =handler.doHandler(request);
		servletResponse.getWriter().print(val);
	}

	@Override
	public String getServletInfo() {
		return null;
	}

	@Override
	public void destroy() {

	}
}
